<template>
    <div class="return">
        支付成功，即将返回.....
    </div>
</template>

<script setup>
import { onMounted } from 'vue'
import axios from "axios"
import { useRouter } from 'vue-router'
const router = useRouter()

// 当前登录的用户id
const storedUser = JSON.parse(localStorage.getItem('loggedInUser'))
console.log("支付成功页面", storedUser)

onMounted(async () => {
    try {
        // 减少就诊数量
        await axios.put(`api/arrange/putArrange/${storedUser.doctor.id}`, {
            doctorId: storedUser.doctor.doctorId,
            officeId: storedUser.doctor.officeId,
            visitorsNumber: storedUser.doctor.visitorsNumber--,
            arrangeDate: storedUser.doctor.arrangeDate,
            cost: storedUser.doctor.cost
        })

        // 新增挂号记录
        await axios.post('api/make/addMake', {
            doctorId: storedUser.doctor.doctorId,
            patientsId: storedUser.id,
            makeDate: storedUser.doctor.arrangeDate
        })

        // 新增支付信息
        const response = await axios.get(`api/alipay/query/${storedUser.orderid}/${storedUser.id}`)
        console.log("新增支付信息", response)

        // await axios.get(`api/alipay/query/${uuid}/${storedUser.id}`)
        // console.log("newPay",`api/alipay/query/${uuid}/${storedUser.id}`)

        // 添加本地缓存
        localStorage.setItem(
            "loggedInUser",
            JSON.stringify({
                id: storedUser.id,
                nickName: storedUser.nickName,
                role: storedUser.role,
            })
        )
        console.log("新本地缓存", JSON.parse(localStorage.getItem('loggedInUser')))

        router.push('/reservation/register')

    } catch (error) {
        console.log("App预约挂号err", error)
    }

})

</script>

<style>
.return {
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30px;
}
</style>